package com.lzs.bootsafety.business.oss.controller;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lzs.bootsafety.business.oss.entity.SysFile;
import com.lzs.bootsafety.business.oss.service.SysFileService;
import com.lzs.bootsafety.common.base.controller.BaseController;
import com.lzs.bootsafety.common.util.Resu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;

@RestController
@RequestMapping("/oss/file")
public class SysFileController extends BaseController {
    @Autowired
    private SysFileService sysFileService;
    @PostMapping("/add")
    public Resu uploadFile(@RequestParam("files") MultipartFile[] files) throws IOException {
        if (null == files || files.length == 0) {
            return Resu.error("至少选则一个");
        }
        int res = sysFileService.upload(files);
        return  res > 0 ? Resu.suces("成功上传"+files.length+"个文件") : Resu.error("上传失败");
    }
    @GetMapping("/download")
    public void download(Long id){
        System.out.println(id);
        if (null!=id){
            sysFileService.download(id);
        }
    }
    @GetMapping("/list")
    public Resu list(@RequestParam("data") String  data){
        SysFile sysFile = JSON.parseObject(data,SysFile.class);
        PageInfo<SysFile> pageInfo = sysFileService.selectSysFileByPage(sysFile);
        return Resu.page(pageInfo.getList(),pageInfo.getTotal(),200);
    }
    @DeleteMapping("/del")
    public Resu del(Long id){
        int r = sysFileService.delByPrimaryKey(id);
        return respDel(r);
    }
}
